package com.ruoyi.system.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.TSpCountIncome;
import com.ruoyi.system.mapper.TSpCountIncomeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TBaseCostMapper;
import com.ruoyi.system.domain.TBaseCost;
import com.ruoyi.system.service.ITBaseCostService;

/**
 * 费用类别Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-21
 */
@Service
public class TBaseCostServiceImpl extends ServiceImpl<TBaseCostMapper, TBaseCost> implements ITBaseCostService
{
    @Autowired
    private TBaseCostMapper tBaseCostMapper;

    /**
     * 查询费用类别
     * 
     * @param costId 费用类别主键
     * @return 费用类别
     */
    @Override
    public TBaseCost selectTBaseCostByCostId(Long costId)
    {
        return tBaseCostMapper.selectTBaseCostByCostId(costId);
    }

    /**
     * 查询费用类别列表
     * 
     * @param tBaseCost 费用类别
     * @return 费用类别
     */
    @Override
    public List<TBaseCost> selectTBaseCostList(TBaseCost tBaseCost)
    {
        List<TBaseCost> tBaseCosts = null;
        if(ObjectUtil.isNotEmpty(tBaseCost.getHisprojectId())){
            tBaseCosts = tBaseCostMapper.selectTBaseCostList(tBaseCost);
            if(tBaseCosts == null || tBaseCosts.size() == 0){
                tBaseCost.setProjectId(tBaseCost.getHisprojectId());
                tBaseCost.setHisprojectId(null);
                tBaseCosts = tBaseCostMapper.selectTBaseCostList(tBaseCost);
            }
        }else {
            tBaseCosts = tBaseCostMapper.selectTBaseCostList(tBaseCost);
        }
        return tBaseCosts;
    }

    /**
     * 新增费用类别
     * 
     * @param tBaseCost 费用类别
     * @return 结果
     */
    @Override
    public int insertTBaseCost(TBaseCost tBaseCost)
    {
        Long count = tBaseCostMapper.selectCount(new LambdaQueryWrapper<TBaseCost>().eq(TBaseCost::getCostName, tBaseCost.getCostName()));
        if(count > 0){
            throw new RuntimeException("费用类别【"+tBaseCost.getCostName() + "】已经存在");
        }
        return tBaseCostMapper.insertTBaseCost(tBaseCost);
    }

    /**
     * 修改费用类别
     * 
     * @param tBaseCost 费用类别
     * @return 结果
     */
    @Override
    public int updateTBaseCost(TBaseCost tBaseCost)
    {
        Long count = tBaseCostMapper.selectCount(new LambdaQueryWrapper<TBaseCost>().eq(TBaseCost::getCostName, tBaseCost.getCostName()));
        if(count > 1){
            throw new RuntimeException("费用类别【"+tBaseCost.getCostName() + "】已经存在");
        }
        return tBaseCostMapper.updateTBaseCost(tBaseCost);
    }

    /**
     * 批量删除费用类别
     * 
     * @param costIds 需要删除的费用类别主键
     * @return 结果
     */
    @Override
    public int deleteTBaseCostByCostIds(Long[] costIds)
    {
        return tBaseCostMapper.deleteTBaseCostByCostIds(costIds);
    }

    /**
     * 删除费用类别信息
     * 
     * @param costId 费用类别主键
     * @return 结果
     */
    @Override
    public int deleteTBaseCostByCostId(Long costId)
    {
        return tBaseCostMapper.deleteTBaseCostByCostId(costId);
    }
}
